package com.lazy.mongo.shw;

import java.util.ArrayList;
import java.util.List;

import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.BasicDBObject;
import com.mongodb.CommandResult;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class PlanDao {

    
    private final static Logger logger = LoggerFactory.getLogger(PlanDao.class);
    
    /**
     * 新增一个计划
     * 
     * @param plan
     */
    public ObjectId add(PosData plan){
        DB db = MongoDb.getInstance().getDb();
        DBCollection coll = db.getCollection(SimulatorConstant.COLL_PLAN);
        DBObject object = DBObjectUtil.getDBObjectByPlan(plan);
        coll.insert(object);
        return (ObjectId)object.get("_id");
    }

    /**
     * 通过ID获得一个plan对象
     * 
     * @param id
     */
    public PosData getById(String id){
        DB db = MongoDb.getInstance().getDb();
        DBCollection coll = db.getCollection(SimulatorConstant.COLL_PLAN);
        DBObject object = coll.findOne(new BasicDBObject("_id", new ObjectId(id)));
        return DBObjectUtil.getPlanByDBObject(object);
    }
    
    public PosData getByHostId(String hostid){
        DB db = MongoDb.getInstance().getDb();
        DBCollection coll = db.getCollection(SimulatorConstant.COLL_PLAN);
        logger.info("Collecton:" + coll.count());
        DBObject object = coll.findOne(new BasicDBObject("hostid", hostid));
        return DBObjectUtil.getPlanByDBObject(object);
    }

    /**
     * 删除一个对象
     * 
     * @param id
     */
    public void delete(String id){
        DB db = MongoDb.getInstance().getDb();
        DBCollection coll = db.getCollection(SimulatorConstant.COLL_PLAN);
        coll.remove(new BasicDBObject("_id", new ObjectId(id)));
    }
    
    public void delete2(String hostid){
        DB db = MongoDb.getInstance().getDb();
        DBCollection coll = db.getCollection(SimulatorConstant.COLL_PLAN);
        coll.remove(new BasicDBObject("hostid", hostid));
    }


    /**
     * 获得所有的plan对象
     * 
     * @return
     */
    public List<PosData> getAll(){
        List<PosData> planList = new ArrayList<PosData>();
        DB db = MongoDb.getInstance().getDb();
        DBCollection coll = db.getCollection(SimulatorConstant.COLL_PLAN);
        DBCursor cursor = coll.find();
        try{
            while(cursor.hasNext()){
                DBObject object = cursor.next();
                planList.add(DBObjectUtil.getPlanByDBObject(object));
            }
        }finally{
            cursor.close();
        }
        return planList;
    }

    /**
     * 修改一个方案
     * 
     * @param plan
     */
    public void modify(PosData plan){
        DB db = MongoDb.getInstance().getDb();
        DBCollection coll = db.getCollection(SimulatorConstant.COLL_PLAN);
        coll.update(new BasicDBObject("_id", new ObjectId(plan.getId())), DBObjectUtil.getDBObjectByPlan(plan));
    }
    
    public void Repair(){
        CommandResult result = MongoDb.getInstance().getDb().command("repairDatabase");
        logger.info("CommandResult:" + result.toString());
    }
}
